package demo.controller;

import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Page;
import demo.domain.CourseSelect;
import demo.domain.Student;
import demo.domain.Teacher;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static jdk.nashorn.internal.objects.NativeMath.log;

@Path(value = "/student",viewPath = "/all/student")

public class StudentController extends Controller {


        public void index() {
            Object username = getSession().getAttribute("username");
            String account=get("username");


            int page=1;
            int size=7;
            if (get("page")!=null&&get("size")!=null){
                page= Integer.parseInt(get("page"));
                size= Integer.parseInt(get("size"));
            }
            set("page",page);
            set("size",size);

            setSessionAttr("page",page);
            setSessionAttr("size",size);

            Page<Teacher> list=null;

                 list= Teacher.dao.paginate(page, size, "select *", " from teacher");




            int totalPage = list.getTotalPage();
            set("totalPage",totalPage);
            List<Teacher> teachers = list.getList();
            list.getPageNumber();

            int total=list.getTotalPage();
            int number=  list.getTotalRow();
            set("teachers",teachers);
            set("number",number);
            set("page",page);
            set("size",size);
            set("total",list.getTotalRow());
            set("account",account);
          //  System.out.println(get("teacherId"));
            render("index.html");
        }


    /**
     * 登陆
     */
    @Clear
    public void signUp(){
        String name=get("username");
        String name1=get("name");
        String sex=get("sex");
        String password=get("password");
        set("name",name);
       // Student student=null;
        boolean b=false;
       // Student student=  Student.dao.findById(name);
        Student student=     Student.dao.findFirst("select * from student where id =?",name);
        System.out.println("student is "+student);
        if (student==null){
            b=true;
            new Student().set("id",name).set("sex",sex).set("password",password).set("name",name).save();
        }

       renderJson(b);

      //  render("signUp.html");
    }

    public void select(){
        String username=  get("username");
        if (username==null){
            username= getSessionAttr("username");
        }
      set("account",username);
      String page=  get("page");
      String size=get("size");
        int a=0;
     // String username=  get("account");
       // System.out.println(get("account"));
     String teacherId= get("teacherId");
     if (teacherId!=null){
       a=  Integer.parseInt(teacherId);
     }
        Teacher teacher = Teacher.dao.findById(a);
        String teacherName=  teacher.getStr("name");
        String subject=teacher.get("subject");

    //  List<CourseSelect> list=  CourseSelect.dao.find("select teacher_id from course_select where student_id=?",username);

        List<CourseSelect> courseSelects=null;
    //  courseSelects= CourseSelect.dao.find("select * from course_select where subject =? and student_id =?",subject,username);
     CourseSelect courseSelect= CourseSelect.dao.findFirst("select * from course_select where subject =? and student_id =?",subject,username);
        System.out.println(courseSelect+"0000");
   if (courseSelect==null||courseSelect.equals("")){
    System.out.println("这是空的");
    new CourseSelect().set("student_id",username)
            .set("teacher_id",teacherId).set("tname",teacherName).set("subject",subject).set("select_time",LocalDateTime.now()).save();
}
       redirect("student?username="+username+"&page="+page+"&size="+size);
    }


    public void selectCourse(){
        String username=  get("username");
        if (username==null){
            username= getSessionAttr("username");
        }
        set("account",username);
        int page=1;
        int size=7;
        if (get("page")!=null&&get("size")!=null){
            page= Integer.parseInt(get("page"));
            size= Integer.parseInt(get("size"));
        }
        set("size",size);
        set("page",page);

        //set("account",get("username"));

       String studentId= get("username");
        Page<CourseSelect> list= CourseSelect.dao.paginate(page, size, "select tname,subject,id,select_time",
                " from course_select where student_id=?",studentId);

        int totalPage = list.getTotalPage();
        set("totalPage",totalPage);
        List<CourseSelect> list1 = list.getList();
        set("courseList",list1);
        set("total",list.getTotalRow());
        render("selectCourse.html");
    }

    public void deleteCourse(){
        int page=1;
        int size=7;
        if (get("page")!=null&&get("size")!=null){
            page= Integer.parseInt(get("page"));
            size= Integer.parseInt(get("size"));
        }
     String courseId=  get("courseId");
        String username=get("username");
        set("size",size);
        set("page",page);
        CourseSelect.dao.deleteById(courseId);
        redirect("selectCourse?username="+username+"&page="+page+"&size="+size);

    }
    public void update(){
        String oldPassword="";
       String username=  get("username");
       if (username==null){
            username= getSessionAttr("username");
       }
        set("account",username);
      String old=  get("old");
      String newp=get("new");
      boolean b=false;
     Student student= Student.dao.findById(username);
     if (student!=null){
         oldPassword= student.getStr("password");
     }
        System.out.println(oldPassword+"oldPassword");
        System.out.println(old+"old");
        if (old!=null&&old.equals(oldPassword)){
            System.out.println("进来到这里");
            b=true;
            Student.dao.findById(username).set("password",newp).update();
            renderJson(b);
        }




    }

   public void search(){
       String content= get("content");
       set("content",content);
//      int page= getSessionAttr("page");
//       int size= getSessionAttr("size");
       int page=1;
       int size=7;
       if (get("page")!=null&&get("size")!=null){
           page= Integer.parseInt(get("page"));
           size= Integer.parseInt(get("size"));
       }


       System.out.println("lalalala");
     // List<Teacher> list= Teacher.dao.find("select * from teacher where subject=?",content);
       Page<Teacher>  list= Teacher.dao.paginate(page, size, "select *", " from teacher where subject like'%"+content+"%'");


       System.out.println("jinguozhehangdaima"+list);
       if (list!=null){
           set("teachers",list.getList());
           set("totalPage",list.getTotalPage());
           set("total",list.getTotalRow());
       }

        render("search.html");
   }
}
